package net.wesley.android.city;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class Ningbo extends City {

	public void requestParamsFinish(String[] values) {
		final DefaultHttpClient httpClient = new DefaultHttpClient();  
		try {
			HttpGet get = new HttpGet("http://wf.nbjj.gov.cn/");
			HttpResponse rsp;
			rsp = httpClient.execute(get);
			BufferedReader br;
			br = new BufferedReader(new InputStreamReader(rsp.getEntity()
					.getContent(),"UTF-8"));
			String line, result = "";
			while ((line = br.readLine()) != null) result += line+"\n";

			HttpPost post=new HttpPost("http://wf.nbjj.gov.cn/wzquery.jsp");
			post.setEntity(new StringEntity("cheHaoMa=%E6%B5%99B%C2%B7"+chepai.substring(2)+"&clsbdm="+values[0]+"&cheClass=02"));
			post.setHeader("Content-Type","application/x-www-form-urlencoded");
			post.setHeader("Referer", "http://wf.nbjj.gov.cn/");
			rsp = httpClient.execute(post);
			String line1, result1 = "";
			br = new BufferedReader(new InputStreamReader(rsp.getEntity()
					.getContent(),"UTF-8"));
			while ((line1 = br.readLine()) != null) result1 += line1+"\n";
			processResult(result1);								

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void processResult(String body) {
		int index=body.indexOf("\"red\" size=\"+1\">");
		if (index>0){
			callback.onComplete(-10001, "ret:"+body.substring(index+16).split("</font>")[0].trim());
			return;
		}
		index=body.indexOf("发现机关</th>");
		if (index>0){
			String str=body.substring(index).split("</table>")[0];
			/*index=str.indexOf("font-weight:700;\" colspan=\"3\">");
			if (index>0){
				callback.onComplete(-10001, "ret:"+str.substring(index+30).split("</td>")[0]);
				return;
			}*/
			
			String[] rows=str.split("<tr");
			String retvalue="";
			for (int i=1;i<rows.length;i++){
				String[] cols=rows[i].split("<td");
				String shijian=cols[3].split("</td>")[0].split(">")[1].replace("&nbsp;", "");
				String didian=cols[4].split("</td>")[0].substring(1).replace("&nbsp;", "");
				String code=cols[5].split("</td>")[0].substring(1).replace("&nbsp;", "");
				retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
			}
			callback.onComplete(rows.length-1, retvalue);
			return;
		}
		else{
			callback.onComplete(0, "");
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("浙B");
	}

	@Override
	public String[] paramDesc() {
		String[] s={"车辆识别代码后6位"};
		return s;
	}

}
